package com.rbac.utils.commUtils;

import javax.servlet.http.HttpServletRequest;
import java.security.MessageDigest;
import java.sql.Timestamp;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;


/***
 * 日常工具类
 */
public class ToolUtil {


    ///返回当前时间
    public static Date getNowDateTime(){
        return new Timestamp(System.currentTimeMillis());
    }


    ///数据去重
    public static Object[] arrayUnique(Object [] arr){
        //实例化一个set集合
        Set set = new HashSet();
        //遍历数组并存入集合,如果元素已存在则不会重复存入
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] == null||arr[i]==""){
                continue;
            }
            set.add(arr[i]);
        }
        //返回Set集合的数组形式
        return set.toArray();
    }


    /***
     * 获得当前的系统时间
     * @return
     */
    public static Date getTodayTime(){
        return new Date();
    }


    ////获得当前系统时间（字符串格式）
    public static String getStringTodayTime(){
        Date todat_date = new Date();
        //将日期格式化
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //转换成字符串格式
        return simpleDateFormat.format(todat_date);
    }

    ///字符串时间转换Date时间
    public static Date getDateTimeByStringTime(String strDate) {
        //如果参数为空，直接返回
        if(strDate == null){ return null; }
        //
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        //
        return formatter.parse(strDate, pos);
    }

    ///Date时间转换字符串时间
    public static String getStringTimeByDateTime(Date t){
        //将日期格式化
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //转换成字符串格式
        return simpleDateFormat.format(t);
    }

    /**
     * MD5方法
     * @param str  待加密的字符串
     * @return  MD5加密后的字符串
     */
    public static String MD5(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] array = md.digest(str.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            for (byte item : array) {
                sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
            }
            return sb.toString();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     *从request中获取用户真实ip
     */
    public static String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /***
     * 检测一个字符串值是否存在 一个字符串数组内
     * @param val
     * @param arr
     * @return
     */
    public static boolean checkStringValExistArr(String val,String[] arr){
        List<String> rulesList = Arrays.asList(arr);
        return rulesList.contains(val);
    }


    //获取加密后的密码
    public static String getEncryptAfterPwd(String init,String salt){
        return MD5(MD5(init+salt)+salt);
    }
}

